package com.itqn.servlet;

import com.alibaba.fastjson.JSONArray;
import com.itqn.pojo.Category;
import com.itqn.pojo.Member;
import com.itqn.service.CategoryService;
import com.itqn.service.Impl.CategoryServiceImpl;
import com.itqn.service.Impl.MemberServiceImpl;
import com.itqn.service.MemberService;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/MemberEchoServlet")
public class MemberEchoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //获取参数
        String id = request.getParameter("id");
        //创建业务逻辑层对象
        MemberService memberService=new MemberServiceImpl();
        Member member = memberService.findById(Integer.parseInt(id));
        String memberstr = JSONArray.toJSONString(member);
        PrintWriter writer = response.getWriter();
        writer.write(memberstr);
    }
}